home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 2000 August: Tool Chest / Dev.CD Aug 00 TC Disk 1.toast / pc / sample code / graphics 2d / seedcfill / seedcfill.c next >
Encoding:
C/C++ Source or Header  |  2000-06-23  |  7.7 KB  |  275 lines

  1. /*
  2.     File:        SeedCFill.c
  3.  
  4.     Contains:    This snippet shows how to use the SeedCFill routine            
  5.                 to create a mask given a source image. As decribed            
  6.                 on page 71 of Inside Mac volume V, this routine                
  7.                 computes a destination bitmap image with 1's only            
  8.                 in the pixels where paint can not leak from the                
  9.                 starting seed point. This is similar to the paint-            
  10.                 bucket tool.        
  11.  
  12.     Written by: Edgar Lee        
  13.  
  14.     Copyright:    Copyright © 1992-1999 by Apple Computer, Inc., All Rights Reserved.
  15.  
  16.                 You may incorporate this Apple sample source code into your program(s) without
  17.                 restriction. This Apple sample source code has been provided "AS IS" and the
  18.                 responsibility for its operation is yours. You are not permitted to redistribute
  19.                 this Apple sample source code as "Apple sample source code" after having made
  20.                 changes. If you're going to re-distribute the source, we require that you make
  21.                 it clear in the source that the code was descended from Apple sample source
  22.                 code, but that you've made changes.
  23.  
  24.     Change History (most recent first):
  25.                 7/14/1999    Karl Groethe    Updated for Metrowerks Codewarror Pro 2.1
  26.                 
  27.  
  28. */
  29.  
  30. #include <Dialogs.h>
  31. #include <Fonts.h>
  32. #include <Palettes.h>
  33. #include <QDOffscreen.h>
  34. #include <Resources.h>
  35.  
  36. /* Constant Declarations */
  37.  
  38. #define    WWIDTH        (176 * 2)
  39. #define    WHEIGHT        (106 * 2)
  40.  
  41. #define WLEFT        (((qd.screenBits.bounds.right - qd.screenBits.bounds.left) - WWIDTH) / 2)
  42. #define WTOP        (((qd.screenBits.bounds.bottom - qd.screenBits.bounds.top) - WHEIGHT) / 2)
  43.  
  44. /* Global Variable Definitions */
  45.  
  46. WindowPtr    gWindow;
  47.  
  48. void initMac();
  49. void createWindow();
  50. void doSeedCFillExample();
  51.  
  52. void DisposeGrafPort();
  53. GrafPtr CreateGrafPort();
  54.  
  55. void doEventLoop();
  56.  
  57. void main(void)
  58. {
  59.     initMac();
  60.     createWindow();
  61.     doEventLoop();
  62. }
  63.  
  64. void initMac()
  65. {
  66.     MaxApplZone();
  67.     
  68.     InitGraf( &qd.thePort );
  69.     InitFonts();
  70.     InitWindows();
  71.     InitMenus();
  72.     TEInit();
  73.     InitDialogs( nil );
  74.     InitCursor();
  75.     FlushEvents( 0, everyEvent );
  76. }
  77.  
  78. void createWindow()
  79. {
  80.     Rect            rect;
  81.     PaletteHandle    palette;
  82.     
  83.     SetRect( &rect, WLEFT, WTOP, WLEFT + WWIDTH, WTOP + WHEIGHT );
  84.     
  85.     gWindow = NewCWindow( 0L, &rect, "\pClick anywhere in left image.", true, documentProc,
  86.                             (WindowPtr)-1L, true, 0L );
  87.                             
  88.     SetPort( gWindow );
  89.  
  90.     /****************************************************************************/    
  91.     /* Attach a palette of the default colortable to the window.                */
  92.     /*  WARNING: SeedCFill may not work if the current gDevice's colortable is     */
  93.     /*    different than that of the offscreen's colortable.  This offscreen is     */
  94.     /*    the one passed into SeedCFill.  To eliminate this problem, we attach a    */
  95.     /*    palette contaning the same colors used by the offscreen to the window.    */
  96.     /****************************************************************************/
  97.     
  98.     palette = NewPalette( 256, GetCTable( 8 ), pmTolerant, 0 );
  99.     SetPalette( gWindow, palette, true );
  100. }
  101.  
  102. void doSeedCFillExample( point )
  103. Point    point;
  104. {
  105.     PicHandle        pict;                /* Pict used for the source. */
  106.     GWorldPtr        source;                /* Gworld used to store the pict. */
  107.     PixMapHandle    sourcePixMap;        /* Handle to the source Gworld. */
  108.     GrafPtr            mask;                /* BitMap used for creating the mask. */
  109.     int                seedH, seedV;        /* Pixel position used to determine the mask. */
  110.     Rect            rect;                /* Bounding rect of mask and source. */
  111.     CGrafPtr        currentPort;        /* Saved CGrafPtr for later restore. */
  112.     GDHandle        currentDevice;        /* Saved device for later restore. */
  113.     
  114.     /* Load the pict resource to be used for the source. */
  115.     pict = (PicHandle)GetResource( 'PICT', 128 );
  116.  
  117.     /* Define the bounding rect for the source and mask. */
  118.     rect = (**pict).picFrame;
  119.     OffsetRect( &rect, -rect.left, -rect.top );
  120.     rect.right *= 2;
  121.     rect.bottom *= 2;
  122.     
  123.     /* Draw the source image in the window to see what the mask will be created from. */
  124.     DrawPicture( pict, &rect );
  125.     HPurge( (Handle)pict );
  126.     
  127.     /* Return if mouse click was not within the source rect. */
  128.     if (point.h < rect.left || point.h > rect.right ||
  129.         point.v < rect.top || point.v > rect.bottom)
  130.             return;
  131.  
  132.     /* Create a gworld to store the pict. */
  133.     NewGWorld( &source, 8, &rect, GetCTable( 8 ), nil, 0 );
  134.     sourcePixMap = GetGWorldPixMap( source );
  135.     
  136.     /* Draw the pict into the gworld. */
  137.     GetGWorld( ¤tPort, ¤tDevice );
  138.     SetGWorld( source, nil );
  139.     DrawPicture( pict, &rect );
  140.     SetGWorld( currentPort, currentDevice );
  141.         
  142.     /* Allocate a bitmap for the mask. */
  143.     mask = CreateGrafPort( &(**sourcePixMap).bounds );
  144.  
  145.     /* Use the mouse click as the starting seed position. */
  146.     seedH = point.h;
  147.     seedV = point.v;
  148.     
  149.     /* Create a mask from the source, starting from the seed point. */
  150.     SeedCFill( (BitMap *)*sourcePixMap, &(*mask).portBits, &(**sourcePixMap).bounds,
  151.                 &(*mask).portRect, seedH, seedV, nil, 0 );
  152.         
  153.     /* Draw a red cross where the mouse was last clicked. */
  154.     ForeColor( redColor );
  155.     MoveTo( rect.left + seedH - 2, seedV );
  156.     LineTo( rect.left + seedH + 2, seedV );
  157.     MoveTo( rect.left + seedH, seedV - 2 );
  158.     LineTo( rect.left + seedH, seedV + 2 );
  159.     ForeColor( blackColor );
  160.     
  161.     OffsetRect( &rect, (**sourcePixMap).bounds.right, 0 );
  162.     EraseRect( &rect );
  163.     
  164.     /* Copy the source to the window while applying the mask. */
  165.     CopyMask( (BitMap *)*sourcePixMap, &((GrafPtr)mask)->portBits, &gWindow->portBits,
  166.                 &(**sourcePixMap).bounds, &((GrafPtr)mask)->portRect, &rect );
  167.     
  168.     /* Release the used memory. */
  169.     DisposeGrafPort( (GrafPtr)mask );
  170.     DisposeGWorld( source );
  171. }
  172.  
  173. GrafPtr CreateGrafPort( bounds )    /* CreateGrafPort originally written by Forrest Tanaka. */
  174. Rect *bounds;
  175. {
  176.     GrafPtr    savedPort;        /* Saved GrafPtr for later restore. */
  177.     GrafPtr    newPort;        /* New GrafPort. */
  178.     Rect    localBounds;    /* Local copy of bounds. */
  179.  
  180.     GetPort( &savedPort );
  181.  
  182.     /* Set the top-left corner of bounds to (0,0). */
  183.     localBounds = *bounds;
  184.     OffsetRect( &localBounds, -bounds->left, -bounds->top );
  185.  
  186.     /* Allocate a new GrafPort. */
  187.     newPort = (GrafPtr)NewPtrClear( sizeof( GrafPort ) );
  188.     
  189.     if (newPort != nil)
  190.     {
  191.         /* Initialize the new port and make the current port. */
  192.         OpenPort( newPort );
  193.  
  194.         /* Initialize and allocate the bitmap. */
  195.         newPort->portBits.bounds = localBounds;
  196.           newPort->portBits.rowBytes = ((localBounds.right + 15) >> 4) << 1;
  197.         newPort->portBits.baseAddr =  NewPtrClear( newPort->portBits.rowBytes *
  198.                                                     (long)localBounds.bottom );
  199.         if (newPort->portBits.baseAddr != nil)
  200.         {
  201.             /* Clean up the new port. */
  202.             newPort->portRect = localBounds;
  203.             ClipRect( &localBounds );
  204.             RectRgn( newPort->visRgn, &localBounds );
  205.             EraseRect( &localBounds );
  206.         }
  207.         else
  208.         {
  209.             /* Allocation failed; deallocate the port. */
  210.             ClosePort( newPort );
  211.             DisposePtr( (Ptr)newPort );
  212.             newPort = nil;
  213.         }
  214.     }
  215.     
  216.     SetPort( savedPort );
  217.     return newPort;
  218. }
  219.  
  220. void DisposeGrafPort( doomedPort )    /* DisposeGrafPort originally written by Forrest Tanaka. */
  221. GrafPtr doomedPort;
  222. {
  223.     ClosePort( doomedPort );
  224.     DisposePtr( doomedPort->portBits.baseAddr );
  225.     DisposePtr( (Ptr)doomedPort );
  226. }
  227.  
  228. void doEventLoop()
  229. {
  230.     EventRecord        event;
  231.     WindowPtr        window;
  232.     short            clickArea;
  233.     Rect              screenRect;
  234.     static Point    point = { -1, -1 };
  235.     
  236.     for (;;)
  237.     {
  238.         if (WaitNextEvent( everyEvent, &event, 0, nil ))
  239.         {
  240.             if (event.what == mouseDown)
  241.             {
  242.                 clickArea = FindWindow( event.where, &window );
  243.                 
  244.                 if (clickArea == inDrag)
  245.                 {
  246.                     screenRect = (**GetGrayRgn()).rgnBBox;
  247.                     DragWindow( window, event.where, &screenRect );
  248.                 }
  249.                 else if (clickArea == inContent)
  250.                 {
  251.                     if (window != FrontWindow())
  252.                         SelectWindow( window );
  253.                     else
  254.                     {
  255.                         point = event.where;
  256.                         GlobalToLocal( &point );
  257.                         doSeedCFillExample( point );
  258.                     }
  259.                 }
  260.                 else if (clickArea == inGoAway)
  261.                     if (TrackGoAway( window, event.where ))
  262.                         return;
  263.             }
  264.             else if (event.what == updateEvt)
  265.             {
  266.                 window = (WindowPtr)event.message;    
  267.                 SetPort( window );
  268.                 
  269.                 BeginUpdate( window );
  270.                 doSeedCFillExample( point );
  271.                 EndUpdate( window );
  272.             }
  273.         }
  274.     }
  275. }